<?php
//模型
class UserDietsModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_diets';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = " where 1 ";
		if(isset($arr['user_id']) && !empty($arr['user_id'])){			
			$where .= " and  (d.status=0 or (d.status=1 and d.user_id='{$arr['user_id']}')) ";
		}
		
		if(isset($arr['uid']) && !empty($arr['uid'])){			
			$where .= " and d.user_id='{$arr['uid']}' ";
		}
		
		
		
		if(isset($arr['status'])){
			$where .= " and  d.status='{$arr['status']}' ";
		}		
		if(isset($arr['type']) && !empty($arr['type'])){
			$where .= " and  d.type='{$arr['type']}' ";
		}
		
		$sql  = "select d.*,u.nickname,u.headimg from ".$this->db->dbprefix.$this->table." as d left join ".$this->db->dbprefix."user as u on d.user_id=u.id {$where}  order by d.addtime desc LIMIT ".($page-1)*$size .",{$size}";
		
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as d ".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists(){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table;
		$query = $this->db->query($sql);
		$res = $query->result_array();
		return $res;		
	}
	//详情
	function get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		if(!$row){
			return false;
		}
		
		$this->load->model('User/UserDietsCommentModel', 'comment', 'index');
		$type = array('','早餐','中餐','晚餐','加餐');//饮食 1早餐 2中餐 3晚餐 4加餐
		$row['datetime'] = date("Y-m-d H:i:s",$row['addtime']);
		$row['type_name']= $type[$row['type']];			
		$comment = $this->comment->get_lists($row['id']);
		$row['comment_list'] = $comment;
		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

